#ifndef _IQUEUE_H_
#define _IQUEUE_H_

#include <stdlib.h>

#define DEFAULT_SIMPLE_QUEUE_SIZE 100

template<typename MEMTYPE>class CSimpleQueue
{
public:
    CSimpleQueue(unsigned int size = DEFAULT_SIMPLE_QUEUE_SIZE);
    ~CSimpleQueue();

    int put(MEMTYPE& in);
    MEMTYPE get();
    
private:
    unsigned int m_readIdx;
    unsigned int m_writeIdx;
    unsigned int m_maxSize;
    unsigned int m_usedSize;
    MEMTYPE* m_pData;
};

template<typename MEMTYPE>
CSimpleQueue<MEMTYPE>::CSimpleQueue(unsigned int size)
{
    m_readIdx = 0;
    m_writeIdx = 0;
    m_maxSize = size;
    m_usedSize = 0;
    m_pData = (MEMTYPE*)malloc(sizeof(MEMTYPE)*size);
}

template<typename MEMTYPE>
CSimpleQueue<MEMTYPE>::~CSimpleQueue()
{
    free(m_pData);
}

template<typename MEMTYPE>
int CSimpleQueue<MEMTYPE>::put(MEMTYPE& in)
{
    if (m_maxSize == m_usedSize)
    {
        return -1;
    }
    m_pData[m_writeIdx] = in;
    m_writeIdx = (m_writeIdx+1)%m_maxSize;
    m_usedSize++;
}

template<typename MEMTYPE>
MEMTYPE CSimpleQueue<MEMTYPE>::get()
{
    if(m_usedSize == 0)
    {
        return -1;
    }
    MEMTYPE getVal = m_pData[m_readIdx];
    m_readIdx = (m_readIdx+1)%m_maxSize;
    m_usedSize--;
    return getVal;
}

#endif
